
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Guicontroller extends CI_Controller {

	var $orgid;

	function __construct()
	{
		parent::__construct();

		$this->orgid=$this->tank_auth->get_user_id();

	}

	public function render(){


		$query=$this->db->select('*')->from('guiattributes')->where("orgid",$this->orgid)->get();

		$totalcharges=$this->input->post('total-hidden-charges',TRUE);

		$data=array();
		foreach($query->result() as $row){
			$data[$row->key]=$row->val;
		}

		$linksquery=$this->db->select('*')->from('categories')->where("orgid",$this->orgid)->where("order >",0)->order_by('order')->get();

		$links=array();
		foreach($linksquery->result() as $row){
			$links[$row->name]=$row->id;
		}

		$data['links']=$links;



		$data['pagelink']=$this->uri->segment(3,0);
		$data['totalcharge']=$totalcharges;
		$this->load->view('header2');
		$this->load->view('customerheader',$data);
		$this->load->view('twocol',$data);
		$this->load->view('customfooter');

	}


	public function loadcustomizer(){
		
		$query=$this->db->select('*')->from('guiattributes')->where("orgid",$this->orgid)->get();
		
		if($query->num_rows()==0){
			
			$query_string="INSERT INTO `guiattributes` (`orgid`, `key`, `val`, `activeind`) VALUES".
			"( $this->orgid , 'header_icon', 'download1.jpg', 0),".
			"( $this->orgid , 'header_title', 'Sunship', 0),".
			"( $this->orgid , 'header_sub_title', 'SunShop', 0),".
			"( $this->orgid , 'footer_title', 'Welcome to Sunshop', 0),".
			"( $this->orgid , 'footer_subtitle', 'powered by ASU SoftEngine', 0),".
			"( $this->orgid , 'footer_copyright', 'Copyright Left GPL V3', 0),".
			"( $this->orgid , 'header_bgcolor', '#215612', 1),".
			"( $this->orgid , 'body_bgcolor', '#cde3f9', 1),".
			"( $this->orgid , 'links_bgcolor', '#123456', 1),".
			"( $this->orgid , 'page_bgcolor', '#ffffff', 1),".
			"( $this->orgid , 'footer_bgcolor', '#efaec9', 1),".
			"( $this->orgid , 'header_title_color', '#66ffa5', 1),".
			"( $this->orgid , 'header_subtitle_color', '#5ea8f3', 1),".
			"( $this->orgid , 'footer_title_color', '#123456', 1),".
			"( $this->orgid , 'footer_subtitle_color', '#123456', 1),".
			"( $this->orgid , 'footer_copyright_color', '#123456', 1),".
			"( $this->orgid , 'sidelink_color', '#ffffff', 1)";
			$this->db->query($query_string);
			
			$query=$this->db->select('*')->from('guiattributes')->where("orgid",$this->orgid)->get();
		}
		
		
		$data=array();
		foreach($query->result() as $row){
			$data[$row->key]=$row->val;
		}

		$this->load->view('guicustomizer.php',$data);
	}

	public function loadcustomizer2(){


		$query=$this->db->select('*')->from('guiattributes')->where("orgid",$this->orgid)->get();
		$data=array();
		foreach($query->result() as $row){
			$data[$row->key]=$row->val;
		}

		$this->load->view('header2');
		$this->load->view('guicustomizer.php',$data);
	}

	public function savetextdata(){
		$data=$this->input->post();

		foreach(array_keys($data) as $key){
			$updateval=array(
				'val'=>$data[$key]
			);
			$this->db->where('orgid',$this->orgid);
			$this->db->where('key',$key);
			$this->db->update('guiattributes',$updateval);
		}

		echo "Successfully Updated.";
	}

	public function saveimagedata(){
		$data=$this->input->post();

		foreach(array_keys($data) as $key){
			$updateval=array(
						'val'=>$data[$key]
			);
			$this->db->where('orgid',1);
			$this->db->where('key',$key);
			$this->db->update('guiattributes',$updateval);
		}

		echo "Image Sucessfully upload.Refresh the page to see the changes.";
	}

	public function getimages(){
		$this->load->helper('file');
		echo json_encode(get_filenames('uploads'));
	}


	public function getactivelinks(){
		$this->db->select('*');
		$this->db->from('categories');
		$this->db->where('orgid',$this->orgid);
		$this->db->where('order >',0);
		$this->db->order_by('order asc');
		$linksquery=$this->db->get();
		$links=array();

		foreach($linksquery->result() as $row){

			$links[$row->name]=$row->id;

		}

		$data['links']=$links;

		echo json_encode($data);
	}


	public function getinactivelinks(){
		$this->db->select('*');
		$this->db->from('categories');
		$this->db->where('orgid',$this->orgid);
		$this->db->where('order',0);
		$this->db->order_by('order asc');
		$linksquery=$this->db->get();
		$links=array();

		foreach($linksquery->result() as $row){

			$links[$row->name]=$row->id;

		}

		$data['links']=$links;

		echo json_encode($data);
	}

	public function saveactivelinks(){
		$data=$this->input->post();

		$i=1;
		$alinks=json_decode($data['activedata']);

		foreach($alinks as $val){
				
			$updateval=array(
				'order'=>$i
			);
			$this->db->where('orgid',$this->orgid);
			$this->db->where('id',$val);
			$this->db->update('categories',$updateval);
			$i++;

		}

		echo "Successfull";

	}

	public function saveinactivelinks(){
		$data=$this->input->post();
		$i=0;
		$inalinks=json_decode($data['inactivedata']);

		foreach($inalinks as $val){
				
			$updateval=array(
				'order'=>$i
			);
			$this->db->where('orgid',$this->orgid);
			$this->db->where('id',$val);
			$this->db->update('categories',$updateval);
		}

		echo "Successfull";
	}


	public function additem(){

		$data=$this->input->post();
		$id=$this->input->post('id',TRUE);

		$cart_string=$this->session->userdata('cart');
		$cart=json_decode($cart_string);

		if(!is_null($cart)) {
			if(array_key_exists($id,$cart)){
				echo "Key exists";
				$datatmp=$cart->$id;
				$datatmp->count +=1;
				print_r($datatmp->count);
				// 				echo "Quantity for ".$id." is ".$datatmp['price'];
				// 				$cart->$id=$datatmp;
					
			}else{
				echo "Key doesnt exist";
				$cart->$id=$data;
					
			}
			//$cart[$id]=$data;
		}else{
			echo "Its null add it";
			$cart->$id=$data;
		}

		$cartjsonstr=json_encode($cart);
		$this->session->set_userdata('cart',$cartjsonstr);
	}

	public function getitems(){

		if(!is_null($this->session->userdata('cart'))){
			$data=$this->session->userdata('cart');
		}else{
			$data=0;
		}

		echo $data;

	}

	public function delitem(){
		$id=$this->input->post('id',TRUE);
		$cart_string=$this->session->userdata('cart');
		$cart=json_decode($cart_string);
		unset($cart->$id);
		$cartjsonstr=json_encode($cart);
		$this->session->set_userdata('cart',$cartjsonstr);
	}


	public function destroysession(){
		$this->session->sess_destroy();
	}
}